#include "LuaInterface.h"

LuaInterface::LuaInterface(void)
{
	L = luaL_newstate();
	luaL_openlibs(L);
}

LuaInterface::~LuaInterface(void)
{
	lua_close(L);
}

bool LuaInterface::loadModule(const char* luaFileName)
{
	int err = luaL_loadfile(L, luaFileName);
	if(LUA_ERRFILE == err)
	{
		printf("%s loadFailed.\n", luaFileName);
		return false;
	}
	if(lua_pcall(L, 0, 0, 0) != 0)
	{
		printf("call module:%s error:%s\n", luaFileName,
				lua_tostring(L, -1));
		return false;
	}
	return true;
	/*
	lua_State* new_L = lua_newthread(L);
	lua_getglobal(new_L, "npc");

	lua_getglobal(L, "dd");
	printf("npc len=%d\n", lua_objlen(new_L, -1));

	
	printf("is table:%d\n", lua_istable(L, -1));
	lua_pushnil(L);	
	while(lua_next(L, -2) != 0)
	{
		printf("%s - %s\n",
			lua_typename(L, lua_type(L, -2)),
			lua_typename(L, lua_type(L, -1)));
		lua_pop(L, 1);
	}
	lua_pop(L, 2);
	*/
}

int LuaInterface::getStackSize()
{
	return lua_gettop(L);
}

int LuaInterface::getInteger(const char* valueName)
{
	lua_getglobal(L, valueName);
	if(!lua_isnumber(L, -1))
	{
		printf("getInteger with name=%s is not number.\n", valueName);
		return 0;
	}
	int value = lua_tointeger(L, -1);
	lua_pop(L, 1);
	return value;
}

float LuaInterface::getFloat(const char* valueName)
{
	lua_getglobal(L, valueName);
	if(!lua_isnumber(L, -1))
	{
		printf("getFloat with name=%s is not number.\n", valueName);
		return 0;
	}
	float value = lua_tonumber(L, -1);
	lua_pop(L, 1);
	return value;
}